package exhibition.app.nexd.cn.presenter;

import android.content.Context;
import android.util.Log;

import com.litesuits.orm.LiteOrm;

import java.util.ArrayList;
import java.util.Arrays;

import exhibition.app.nexd.cn.application.NexdApplication;
import exhibition.app.nexd.cn.model.Hire;
import exhibition.app.nexd.cn.ui.ivew.ILoadingActivityView;

/**
 * Created by wangxu on 16/4/21.
 */
public class LoadingPresenter extends BasePresenter<ILoadingActivityView> {


    LiteOrm liteOrm;

    public LoadingPresenter(Context context, ILoadingActivityView iView) {
        super(context, iView);
    }

    public void getHireFromDB() {
        liteOrm = NexdApplication.getInstance().getLiteOrm();
        ArrayList<Hire> hireList = liteOrm.query(Hire.class);

        if (hireList == null || hireList.size() == 0) {
            initHireData();
            getHireFromDB();
            return;
        }
        NexdApplication.getInstance().setHires(hireList);
        Log.d(this.getClass().getSimpleName(), Arrays.toString(hireList.toArray()));
    }

    private void initHireData() {
        NexdApplication.getInstance().getHires().clear();
        for (int i = 0; i < 20; i++) {
            //情况1：有剩余存货
            Hire hire = new Hire("租赁商品" + (i + 1), 500, 0, 3.00, (i + 1));
            NexdApplication.getInstance().getHires().add(hire);
        }
        liteOrm.save(NexdApplication.getInstance().getHires());
        Log.d(this.getClass().getSimpleName(), "数据入库");
        Log.d(this.getClass().getSimpleName(), Arrays.toString(NexdApplication.getInstance().getHires().toArray()));
    }
}
